/*
* Copyright (C) 2009-2013 - Roman Kalynchuk
*
* This file is part of REngine.
*
* REngine is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* REngine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with REngine.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "kernel/ilog.h"
#include "kernel/kerneltypes.h"
#include "windowsdl.h"

namespace rengine3d {

	CWindowSDL::CWindowSDL():m_fullScreen(false), m_title("engine"), m_bpp(32){
		m_width		= 800;
		m_height	= 600;
	}

	CWindowSDL::~CWindowSDL(){
	}

	bool CWindowSDL::Init(const string_t& title, uint width, uint height, uint bpp, bool fullScreen){
		if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0) {
			Error("Failed init to SDL !\n");
			return false;
		}
		m_width		= width;
		m_height	= height;
		m_bpp		= bpp;
		m_fullScreen= fullScreen;

		m_screen = SDL_SetVideoMode(width, height, bpp, fullScreen ? SDL_OPENGL |SDL_FULLSCREEN: SDL_OPENGL);

		return (m_screen != NULL);
	}

	void CWindowSDL::Shutdown(void){
		SDL_Quit();
	}

	bool CWindowSDL::Update(void){
		SDL_Event	sdlEvent;
		bool		result;

		if ( SDL_PollEvent(&sdlEvent) ){
			//m_events.push_back(sdlEvent);
			switch ( sdlEvent.type )
			{
			case SDL_QUIT:
				int a =0;
				break;
			};
			result = true;
		}else {
			result = false;
		}

		return result;
	}

	void CWindowSDL::SwapBuffers(void) {
		SDL_GL_SwapBuffers();
	}

	void CWindowSDL::SetFullScreen(bool fs){
		m_screen = SDL_SetVideoMode(m_width, m_height, m_bpp, fs ? SDL_OPENGL |SDL_FULLSCREEN: SDL_OPENGL);
	}

	int CWindowSDL::GetXPosition(void) {
		int x,y;
		SDL_GetMouseState(&x, &y);
		return x;
	}

	int CWindowSDL::GetYPosition(void) {
		int x,y;
		SDL_GetMouseState(&x, &y);
		return y;
	}

	bool CWindowSDL::IsActive(void) {
		uint state = SDL_GetAppState();
		return (state & SDL_APPACTIVE) ? true: false;
	}

	void CWindowSDL::SetActive(bool active){

	}

	void CWindowSDL::SetFocus(bool focus){

	}

	bool CWindowSDL::GetFocus(void) {
		return true;
	}

	void CWindowSDL::SetCursorPos(uint x, uint y){
		SDL_WarpMouse(x, y);
	}

	void CWindowSDL::ShowCursor(void){
		SDL_ShowCursor(SDL_ENABLE);
	}

	void CWindowSDL::HideCursor(void){
		SDL_ShowCursor(SDL_DISABLE);
	}

	void CWindowSDL::SetTitle(const string_t& title){
		SDL_WM_SetCaption(title.c_str(), NULL);
	}

}